#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Premiere.DTO;
using Premiere.Window;
using System.Collections;

#endregion

namespace PremiereMX
{
    public partial class UserControlLogin : UserControl, IUserControlBase
    {
        UserDTO m_user;
        IMainForm m_mainform;
        GeneralSetUpDTO m_generalSetup;

        public UserControlLogin(IMainForm mainform)
        {
            InitializeComponent();
            m_mainform = mainform;
            mainform.UpdateButton(null);
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                ClearErrorProvider();
              //  if (isValid())
              //  {
                    //m_user = (UserDTO)ServiceRegistry.GetInstance().GetUserService().ConfirmUser(usertextBox.Text.Trim(), passwordtextBox.Text.Trim());
                    m_user = (UserDTO)ServiceRegistry.GetInstance().GetUserService().ConfirmUser("ADMIN", "12345");
                    clearForm();
                    PeriodDTO period = (PeriodDTO)ServiceRegistry.GetInstance().GetPeriodService().GetCurrentPeriod();
                    if (period == null)
                        MessageBox.Show("Current Period is not define", "Warning Period");
                    else
                        if (!period.IsInRange(DateTime.Today))
                            MessageBox.Show("Computer DateTime ( " + DateTime.Today.ToString("dd-MMM-yyyy") + " ) is not in the active Month ( " + period.ToString() + " )", "Warning Period");
                    m_mainform.SetUser(m_user);
                    m_mainform.SetPeriod(period);
                    IList generalresult = ServiceRegistry.GetInstance().GetGeneralSetupService().LoadAll();
                    m_generalSetup = generalresult.Count == 0 ? new GeneralSetUpDTO() : generalresult[0] as GeneralSetUpDTO;
                    m_mainform.SetGeneralSetup(m_generalSetup);
                   
              //  }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
                passwordtextBox.Focus();
                passwordtextBox.SelectAll();
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void ClearErrorProvider()
        {
            errorProvider1.SetError(usertextBox, "");
            errorProvider1.SetError(passwordtextBox, "");
        }
        private bool isValid()
        {
            bool valida = !usertextBox.Text.Trim().Equals(string.Empty);
            bool validb = !passwordtextBox.Text.Trim().Equals(string.Empty);
            if (!valida)
                errorProvider1.SetError(usertextBox, "Fill User ID");
            if (!validb)
                errorProvider1.SetError(passwordtextBox, "Fill Password");
            return valida && validb;
        }
        private void clearForm()
        {
            usertextBox.Text = string.Empty;
            passwordtextBox.Text = string.Empty;
        }

        #region IUserControlBase Members

        public void SetMainForm(IMainForm mainform)
        {
            m_mainform = mainform;
        }

        public void UpdateButton()
        {
            m_mainform.UpdateButton(null);
        }

        public void Init()
        {
            throw new NotImplementedException();
        }

        public void Refresh()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IUserControlBase Members


        public void ClearForm()
        {
            
        }

        public void SetToReadOnly(bool ro)
        {
            
        }

        public void SetButtonEditMode(EditMode editMode)
        {
            
        }

        public void RemoveItem(long id)
        {
            throw new NotImplementedException();
        }
        public void FocusToCode()
        {
            usertextBox.Focus();
        }
        public bool IsValidSubject()
        {
            return true;
        }

        #endregion
    }
}